<?php
class SJKObj {
    protected $table;
    protected $sjk;
    public function __construct() {
    }
    public function __call($function,$args) {
        if (strpos($function,'getBy')===0) {
            $field=strtolower(substr($function,5));
            if (!empty($args[1])) {
                $select_fields=$args[1];
            } else {
                $select_fields='*';
            }
            return $this->get(array($field=>$args[0]), $select_fields);
        } else if (strpos($function,'getListBy')===0) {
            $field=strtolower(substr($function,9));
            if (!empty($args[4])) {
                $select_fields=$args[4];
            } else {
                $select_fields='*';
            }
            if (!empty($args[3])) {
                $order=$args[3];
            } else {
                $order=null;
            }
            return $this->getList(array($field=>$args[0]), $args[1], $args[2], $order, $select_fields);
        } else if (strpos($function,'getAllBy')===0) {
            $field=strtolower(substr($function,8));
            if (!empty($args[1])) {
                $select_fields=$args[1];
            } else {
                $select_fields='*';
            }
            if (!empty($args[3])) {
                $order=$args[3];
            } else {
                $order=null;
            }
            return $this->getAll(array($field=>$args[0]), $order, $select_fields);
        } else if (strpos($function,'getTotalBy')===0) {
            $field=strtolower(substr($function,10));
            if (!empty($args[1])) {
                $select_fields=$args[1];
            } else {
                $select_fields='*';
            }
            return $this->getTotal(array($field=>$args[0]), $select_fields);
        }
    }
    public function getAll($wheres=array(), $order=null, $select_fields='*') {
        if (!empty($order)) {
            $order=' ORDER BY '.$order;
        }
        $this->sjk->initSql('SELECT '.$select_fields.' FROM '.$this->table.
                            ' '.$this->buildWhere($wheres).$order);
        $rs=$this->sjk->query();
        return $rs->fetchAll();
    }
    public function getList($wheres, $offset, $limit, $order=null, $select_fields='*') {
        if (!empty($order)) {
            $order=' ORDER BY '.$order;
        }
        $this->sjk->initSql('SELECT '.$select_fields.' FROM '.$this->table.
                            ' '.$this->buildWhere($wheres).$order.' LIMIT '.$offset.','.$limit);
        $rs=$this->sjk->query();
        return $rs->fetchAll();
    }
    public function getTotal($wheres=array(), $select_fields='*') {
        $this->sjk->initSql('SELECT count('.$select_fields.') as c FROM '.$this->table.
                            ' '.$this->buildWhere($wheres));
        $rs=$this->sjk->query();
        $row=$rs->fetch();
        return $row['c'];
    }
    public function get($wheres=array(), $select_fields='*') {
        $this->sjk->initSql('SELECT '.$select_fields.' FROM '.$this->table.
                            ' '.$this->buildWhere($wheres).' LIMIT 1');
        $rs=$this->sjk->query();
        if ($rs->num()>0) {
            $row=$rs->fetch();
            return $row;
        } else {
            return false;
        }
    }
    public function add($data_arr) {
        $this->sjk->initSql('INSERT INTO '.$this->table.' #1');
        $this->sjk->setInsertArray(1, $data_arr);
        $this->sjk->query();
        return $this->sjk->getInsertId();
    }
    private function buildWhere($wheres) {
        if (!is_array($wheres)) {
            return '';
        }
        $where='';
        foreach ($wheres as $field => $value) {
            $where.='AND `'.$field."`='".$this->sjk->escape($value)."'";
        }
        if (strlen($where)>0) {
            return 'WHERE '.substr($where,3);
        } else {
            return '';
        }
    }
}
